/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package siswa;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rendra
 */
public class Criteria extends javax.swing.JInternalFrame {
    private DefaultTableModel model;
    private String id_detil;

    /**
     * Creates new form Criteria
     */
    public Criteria() {
        initComponents();
        loadDataset();
        getTableValues();
    }
    
    private void loadDataset() {
        try {
            Statement stmt = App.conn.createStatement();            
            stmt.executeQuery("SELECT a.id_kriteria_detil AS NO, a.id_kriteria AS ID, "+
                    "b.nama_kriteria AS KRITERIA, a.detil_ket AS KET, a.nil_a AS AWAL, "+
                    "a.nil_b AS AKHIR FROM kriteria_detil a " +
                    "LEFT JOIN kriteria b ON b.id_kriteria = a.id_kriteria");
            
            ResultSet rs = stmt.getResultSet();
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            
            Vector data = new Vector(columns);
            Vector rows = new Vector(columns);
            Vector name = new Vector(columns);

            int k = 0;
            while (rs.next()) {
                if (k == 0) {
                    for (int i=1;i<=columns;i++) {
                        name.addElement(md.getColumnLabel(i));
                    }
                }
                for (int j=1;j<=columns;j++) {
                    rows.addElement(rs.getObject(j));
                }
                data.addElement(rows);
                rows = new Vector(columns);
                
                k++;
            }

            model = new DefaultTableModel(data, name);
            CriteriaTable.setModel(model);
            CriteriaTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            int gapWidth = 10;
            int gapHeight = 5;
            CriteriaTable.setIntercellSpacing(new Dimension(gapWidth, gapHeight));
            CriteriaTable.setRowHeight(20);
            CriteriaTable.getTableHeader().setFont(new Font(null, Font.BOLD, 12));
            
            ListSelectionModel selectionModel = CriteriaTable.getSelectionModel();
            selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            selectionModel.addListSelectionListener(CriteriaTable);
        } catch (SQLException ex) {
            Logger.getLogger(Criteria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void getTableValues() {
        CriteriaTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                int row = CriteriaTable.getSelectedRow();
                n_dari.setText(CriteriaTable.getModel().getValueAt(row, 4).toString());
                n_sampai.setText(CriteriaTable.getModel().getValueAt(row, 5).toString());
                id_detil = (CriteriaTable.getModel().getValueAt(row, 0).toString());
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TableScroll = new javax.swing.JScrollPane();
        CriteriaTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        n_dari = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        n_sampai = new javax.swing.JTextField();
        UpdBtn = new javax.swing.JButton();

        CriteriaTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TableScroll.setViewportView(CriteriaTable);

        jLabel1.setText("Ubah range nilai dari ");

        n_dari.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText("hingga");

        n_sampai.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        UpdBtn.setText("Simpan Perubahan");
        UpdBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TableScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(n_dari, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(n_sampai, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(UpdBtn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(TableScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(n_dari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(n_sampai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(UpdBtn)
                .addContainerGap(57, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void UpdBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdBtnActionPerformed
        // TODO add your handling code here:
        if (!n_dari.getText().equals("") && !n_sampai.getText().equals("")) {
            try {
                String nil_a = n_dari.getText();
                String nil_b = n_sampai.getText();
                Statement stmt = App.conn.createStatement();            
                stmt.executeUpdate("UPDATE kriteria_detil SET nil_a = '"+nil_a+"', nil_b='"+nil_b+"' WHERE id_kriteria_detil='"+id_detil+"'");
                model.setRowCount(0);
                loadDataset();
                n_dari.setText("");
                n_sampai.setText("");
                JOptionPane.showMessageDialog(null, "Data berhasil dirubah", "Informasi", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                Logger.getLogger(Criteria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_UpdBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable CriteriaTable;
    private javax.swing.JScrollPane TableScroll;
    private javax.swing.JButton UpdBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField n_dari;
    private javax.swing.JTextField n_sampai;
    // End of variables declaration//GEN-END:variables
}
